﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Validator
/// </summary>
public static class Validator
{
    /// <summary>
    /// Check that the path leads to the model folder
    /// </summary>    
    public static bool ValidatePath(string a_path, string[] a_folders, string[] a_extensions = null)
    {
        //Check that path is at least 3 letters,  a.ext
        //Check that path doesn't contain .. to go elsewhere somehow
        //Check path starts with a path from a_folders
        //Check the file extension

        if (a_path == null)
        {
            return false;
        }

        //Check if it starts with bad characters
        if (a_path.StartsWith(".") || a_path.StartsWith("/") || a_path.StartsWith("\\"))
        {
            return false;
        }

        //If path has .. it's not valid
        if (a_path.Contains(".."))
        {
            return false;
        }

        bool f_validPathStart = false;
        for (int i = 0; i < a_folders.Length; i++)
        {
            if (a_path.StartsWith(a_folders[i]))
            {
                f_validPathStart = true;
                break;
            }
        }
        //If path doesn't start with a_folders 
        if (!f_validPathStart)
        {
            return false;
        }

        if (a_extensions != null)
        {
            if (!ValidateFileExtension(a_path, a_extensions))
            {
                return false;
            }
        }

        return true;
    }

    /// <summary>
    /// Checks that filename ends with a given extension
    /// </summary>    
    public static bool ValidateFileExtension(string a_filename, string[] a_validExtensions)
    {
        for (int i = 0; i < a_validExtensions.Length; i++)
        {
            if (a_filename.EndsWith(a_validExtensions[i]))
            {
                return true;
            }
        }

        return false;
    }
}